{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "09770f10",
   "metadata": {},
   "source": [
    "# 无监督聚类计算ITHScore\n",
    "\n",
    "* habitat_dir: 你的生境区域的nii.gz文件夹"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7ced4169",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "import nibabel as nib\n",
    "import pandas as pd\n",
    "from onekey_algo.custom.components.habitat import cal_unsupervised_ITHscore\n",
    "\n",
    "# 你自己的生境聚类结果的位置。\n",
    "habitat_dir = r'C:\\Users\\onekey\\Desktop\\demo\\habitat\\cluster_3'\n",
    "ith = []\n",
    "for sample in os.listdir(habitat_dir):\n",
    "    if not sample.endswith('.nii.gz'):\n",
    "        continue\n",
    "    arr = np.array(nib.load(os.path.join(habitat_dir, sample)).dataobj)\n",
    "    ith.append([sample, cal_unsupervised_ITHscore(arr)])\n",
    "\n",
    "ith = pd.DataFrame(ith, columns=['ID', 'ITHScore'])\n",
    "ith.to_csv('ith_score.csv', index=False)\n",
    "ith"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "169e9408",
   "metadata": {},
   "source": [
    "# 转化成评估格式\n",
    "\n",
    "有label-0和label-1两列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c6568082",
   "metadata": {},
   "outputs": [],
   "source": [
    "ith.columns=['ID', 'label-1']\n",
    "ith['label-0'] = 1 - ith['label-1']\n",
    "ith[['ID', 'label-0', 'label-1']].to_csv('ith_score4eval.csv', index=False)\n",
    "ith"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d3b8158",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
